package com.hexiang.service;

import com.hexiang.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

@Service
@Slf4j
public class AsyncService {

    private final Logger logger = LoggerFactory.getLogger(AsyncService.class);

    @Async
    public void async() {
        logger.info("async start");
//        throw new RuntimeException("参数不合法");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            logger.error("async exp", e);
        }
        logger.info("async end");
    }

    @Async
    public Future<UserVO> future1(long id) {
        UserVO user = new UserVO();
        user.setId(id);
        user.setName("name" + id);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            logger.error("async exp", e);
        }
        return new AsyncResult<UserVO>(user);
    }

    @Async
    public Future<UserVO> future2(long id) {
        UserVO user = new UserVO();
        user.setId(id);
        user.setName("name" + id);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            logger.error("async exp", e);
        }
        return new AsyncResult<UserVO>(user);
    }

    @Async
    public Future<UserVO> future3(long id) {
        UserVO user = new UserVO();
        user.setId(id);
        user.setName("name" + id);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            logger.error("async exp", e);
        }
        return new AsyncResult<UserVO>(user);
    }

    public Future<String> getUserInfoByFuture(String userName) {
        String userInfo = "";
        try {
            if (userName.contains("aa")) {
                throw new RuntimeException("参数不合法");
            }
            try {
                Thread.sleep(1000);
                userInfo = userName + ",ok";
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
                e.printStackTrace();
            }
        } catch (Exception e) {
            log.error("getUserInfoByCompletableFutureErr", e);
            userInfo = userName + ",err";
        }
        return new AsyncResult<>(userInfo);
    }

    @Async
    public CompletableFuture<String> getUserInfoByCompletableFuture(String userName) {
        CompletableFuture<String> completableFuture = new CompletableFuture();
        String userInfo = "";
        try {
            if (userName.contains("aa")) {
                throw new RuntimeException("参数不合法");
            }
            try {
                Thread.sleep(1000);
                userInfo = userName + ",ok";
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
                e.printStackTrace();
            }
            completableFuture.complete(userInfo);
        } catch (Exception e) {
            log.error("getUserInfoByCompletableFutureErr", e);
            userInfo = userName + ",err";
            completableFuture.complete(userInfo);
        }

        return completableFuture;
    }
}
